<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f0f0f0;
        }

        .container {
            width: 300px;
            margin: 50px auto;
            padding: 20px;
            background-color: white;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }

        .form-title {
            text-align: center;
            font-size: 24px;
            margin-bottom: 20px;
        }

        input[type="text"], input[type="password"] {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border-radius: 4px;
            border: 1px solid #ccc;
            box-sizing: border-box;  /* 确保输入框包含内边距和边框 */
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }

        button:hover {
            background-color: #0056b3;
        }

        .switch-link {
            display: block;
            text-align: center;
            margin-top: 10px;
            color: #007bff;
        }

        .switch-link a {
            color: #007bff;
            text-decoration: none;
        }

        .switch-link a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>

<div class="container">
    <div class="form-title">登录</div>
    <form id="login-form" onsubmit="event.preventDefault(); login();">
        <input type="text" id="login-username" placeholder="用户名/邮箱" required>
        <input type="password" id="login-password" placeholder="密码" required>
        <button type="submit">登录</button>
    </form>
    <div class="switch-link">
        还没有账户？<a href="./register.html">注册</a>
    </div>
</div>

<script>
    // 发送登录请求
    function login() {
        const username = document.getElementById('login-username').value;
        const password = document.getElementById('login-password').value;

        // 请求数据
        const requestData = {
            username: username,
            password: password
        };

        // 使用 fetch 向后端提交请求
        fetch('http://localhost:8090/client/user/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(requestData)
        })
        .then(response => {
            if (response.ok) {
                return response.json();
            }
            throw new Error('登录失败');
        })
        .then(data => {
            if (data.code == 200) {
                alert(`登录成功！用户名: ${username}`);
                window.location.href = './chat.html';
            } else {
                alert('登录失败：' + data.msg);
            }
        })
    }
</script>

</body>
</html>
